

from PyQt5.QtCore import QDate 
from base.window.base_window import BaseDialog
from utils.time_utils import TimeUtils


class DateTimeEdit(BaseDialog):
  def __init__(self, parent=None, type="time_range"):
    self.type = type
    self.start_hour = TimeUtils.get_current_datetime().hour
    self.start_minute = 0
    self.end_hour = TimeUtils.get_current_datetime().hour
    self.end_minute = 0
    self.mydate = TimeUtils.get_current_date()
    self.data = None
    super(DateTimeEdit, self).__init__(title="日期时间选择", log_name="tools", ui_path="date_time_edit.ui", rate = 0.5)

    self.load_page()

    self.init_event()

  def init_event(self):
    # super().init_events()
    self.cb_start_hour.currentIndexChanged.connect(lambda : self.onIndexChanged(1))
    self.cb_start_minute.currentIndexChanged.connect(lambda : self.onIndexChanged(2))
    self.cb_end_hour.currentIndexChanged.connect(lambda : self.onIndexChanged(3))
    self.cb_end_minute.currentIndexChanged.connect(lambda : self.onIndexChanged(4))

    self.ui.cal_date_edit.selectionChanged.connect(lambda : self.onDateChanged())

  def onIndexChanged(self, flag):
    if flag == 1:
      self.start_hour = int(self.ui.cb_start_hour.currentText())
    elif flag == 2:
      self.start_minute = int(self.ui.cb_start_minute.currentText())
    elif flag == 3:
      self.end_hour = int(self.ui.cb_end_hour.currentText())
    else:
      self.end_minute = int(self.ui.cb_end_minute.currentText())

    # self.data = {"start_hour": self.start_hour, "start_minute": self.start_minute, "end_hour": self.end_hour, "end_minute": self.end_minute}

  def onDateChanged(self):
    self.mydate = self.ui.cal_date_edit.selectedDate().toString('yyyy-MM-dd')


  def load_page(self):
    if self.type == "time_range":
      self.ui.sw_datetime.setCurrentIndex(1)
      for i in range(24):
        self.ui.cb_start_hour.addItem("%d" % i)
        self.ui.cb_end_hour.addItem("%d" % i)
        self.ui.cb_start_hour.setCurrentText(str(TimeUtils.get_current_datetime().hour))
        self.ui.cb_end_hour.setCurrentText(str(TimeUtils.get_current_datetime().hour))
      for i in range(60):
        self.ui.cb_start_minute.addItem("%d" % i)
        self.ui.cb_end_minute.addItem("%d" % i)
        self.ui.cb_start_minute.setCurrentText("0")
        self.ui.cb_end_minute.setCurrentText("0")
    elif self.type == "date":
      self.ui.sw_datetime.setCurrentIndex(0)
      self.ui.cal_date_edit.setMinimumDate(QDate(2020, 1, 1))
      self.ui.cal_date_edit.setMaximumDate(QDate(2100, 12, 31))
      self.ui.cal_date_edit.setSelectedDate(QDate.currentDate())

  def set_data(self, data):
    self.data = data

  def get_data(self):
    self.data = {"start_hour": self.start_hour, 
                 "start_minute": self.start_minute, 
                 "end_hour": self.end_hour, 
                 "end_minute": self.end_minute,
                 "mydate": self.mydate
                 }
    return self.data

  # def closeEvent(self, event):
  #   # 在这里添加您想要在窗口关闭时执行的代码
  #   self.log.debug("[DateTimeEdit]窗口正在关闭...")
  #   result = {"start_hour": self.ui.cb_start_hour.currentText(), 
  #             "start_minute": self.ui.cb_start_minute.currentText(), 
  #             "end_hour": self.ui.cb_end_hour.currentText(), 
  #             "end_minute": self.ui.cb_end_minute.currentText(),
  #             "mydate": self.ui.cal_date_edit.selectedDate().toString('yyyy-MM-dd')}
  #   self.set_data(result)